package org.linlinjava.litemall.admin.web;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Hash;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.GoodsAllinone;
import org.linlinjava.litemall.admin.service.AdminGoodsService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallBrandService;
import org.linlinjava.litemall.db.service.LitemallCategoryService;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
@Validated
public class AdminGoodsController {
    private final Log logger = LogFactory.getLog(AdminGoodsController.class);

    @Autowired
    private AdminGoodsService adminGoodsService;
    @Autowired
    private LitemallGoodsService litemallGoodsService;
    @Autowired
    private LitemallGoodsProductService litemallGoodsProductService;
    @Autowired
    private LitemallCategoryService litemallCategoryService;
    @Autowired
    private LitemallBrandService litemallBrandService;

    /**
     * 查询商品
     *
     * @param goodsId
     * @param goodsSn
     * @param name
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(Integer goodsId, String goodsSn, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return adminGoodsService.list(goodsId, goodsSn, name, page, limit, sort, order);
    }

    @GetMapping("/catAndBrand")
    public Object list2() {
        return adminGoodsService.list2();
    }

    /**
     * 编辑商品
     *
     * @param goodsAllinone
     * @return
     */
    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone) {
        return adminGoodsService.update(goodsAllinone);
    }

    /**
     * 删除商品
     *
     * @param goods
     * @return
     */
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallGoods goods) {
        return adminGoodsService.delete(goods);
    }

    /**
     * 添加商品
     *
     * @param goodsAllinone
     * @return
     */
    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "上架")
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone) {
        return adminGoodsService.create(goodsAllinone);
    }

    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:goods:read")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "详情")
    @GetMapping("/detail")
    public Object detail(@NotNull Integer id) {
        return adminGoodsService.detail(id);

    }

    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "导出")
    @GetMapping("/toExcel")
    public Object excel() {
        List<Map<String,Object>> data=new ArrayList<>();
        List<LitemallGoods> list=litemallGoodsService.all();
        List<LitemallCategory> categorys = litemallCategoryService.queryL2();
        Map<Integer,Object> categoryMap=new HashMap<>();
        for (int i=0;i<categorys.size();i++){
            categoryMap.put(categorys.get(i).getId(),categorys.get(i).getName());
        }
        List<LitemallBrand> bands= litemallBrandService.all();
        Map<Integer,Object> brandMap=new HashMap<>();
        for (int i=0;i<bands.size();i++){
            brandMap.put(bands.get(i).getId(),bands.get(i).getName());
        }
        for (int i=0;i<list.size();i++){
            LitemallGoods good= list.get(i);
            List<LitemallGoodsProduct> products = litemallGoodsProductService.queryByGid(good.getId());
            for (int j=0;j<products.size();j++){
                LitemallGoodsProduct product=products.get(j);
                Map<String,Object> map=new HashMap<>();
                map.put("id",good.getId());//商品ID
                map.put("name",good.getName());//名称
                map.put("isNew",good.getIsNew()?"是":"否");//是否新品
                map.put("isHot",good.getIsHot()?"是":"否");//是否热品
                map.put("isOnSale",good.getIsHot()?"在售":"下架");//是否在售
                map.put("keywords",good.getKeywords());//关键字
                map.put("category",categoryMap.get(good.getCategoryId()));//类目
                map.put("brand",brandMap.get(good.getBrandId()));//商品品牌
                map.put("product", ArrayUtils.toString(product.getSpecifications(), ",").replace("{","").replace("}",""));//规格
                map.put("price",product.getPrice());//价格
                data.add(map);
            }
        }

        return ResponseUtil.ok(data);
    }

}
